package com.zry.simpleBlog.dto;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.zry.simpleBlog.comment.aop.annotations.IdempotentField;
import com.zry.simpleBlog.comment.strategy.idempotent.IdempotentInterface;
import com.zry.simpleBlog.entity.Comment;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.util.StringUtils;

import javax.validation.constraints.NotBlank;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 14470
 */
@Data
@NoArgsConstructor
public class CommentDTO implements Serializable, IdempotentInterface {

    private String id;

    @IdempotentField
    @NotBlank(message = "昵称不能为空")
    private String nickname;

    @IdempotentField
    private String email;

    @IdempotentField
    @NotBlank(message = "内容不能为空")
    private String content;

    @IdempotentField
    @Schema(example = "/images/avatar/avatar1.png")
    private String avatar;

    @IdempotentField
    @Schema(example = "{id: 1}")
    private CommentDTO parentComment;

    private String creatorRole;

    @Schema(hidden = true)
    @JsonFormat(pattern = "yyy-MM-dd HH:mm", timezone = "GMT+8")
    private Date createTime;


    @IdempotentField
    private String blogId;


    @Schema(hidden = true)
    private List<CommentDTO> replyComments = new ArrayList<>();


    public Comment parseComment() {
        Comment comment = new Comment();
        comment.setId(this.id);
        comment.setCreatorRole(this.creatorRole);
        comment.setContent(this.content);
        if (this.parentComment == null || StringUtils.isEmpty(this.parentComment.getId())) {
            comment.setParentCommentId(null);
        } else {
            comment.setParentCommentId(this.parentComment.getId());
        }
        comment.setEmail(this.email.trim());
        comment.setBlogId(this.blogId);
        comment.setCreateTime(this.createTime);
        comment.setNickname(this.nickname);
        comment.setAvatar(this.avatar);
        return comment;
    }
}
